package com.kk.utils;

import java.util.UUID;

/**
 * @author kl
 * @version 1.0
 * @description: TraceId工具类
 * @date 2025/10/17 下午2:51
 */
public class TraceIdUtils {
    private static final ThreadLocal<String> TRACE_ID = new ThreadLocal<>();

    /**
     * 生成TraceId（无横线的UUID）
     */
    public static String generateTraceId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 设置TraceId
     */
    public static void setTraceId(String traceId) {
        TRACE_ID.set(traceId);
    }

    /**
     * 获取TraceId
     */
    public static String getTraceId() {
        String traceId = TRACE_ID.get();
        if (traceId == null) {
            traceId = generateTraceId();
            TRACE_ID.set(traceId);
        }
        return traceId;
    }

    /**
     * 清除TraceId
     */
    public static void clearTraceId() {
        TRACE_ID.remove();
    }
}